﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models;
using System.Data.SqlClient;
namespace jiaju.Areas.Back.Models.DAL
{
    public class NewsTypeService
    {
        public static List<NewsType> getNewsTypeList(int page, int limit, string name)
        {
            string sql = string.Format("select top({0}) * from dbo.newsType where Id not in (select top(({1}-1) * {0}) Id from dbo.newsType)", limit,page);
            if (!string.IsNullOrEmpty(name))
            {
                sql = sql + string.Format("and Name like '%{0}%'", name);
            }
            List<NewsType> list = new List<NewsType>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];             
                NewsType newtype = new NewsType()
                {
                    Id = Id,
                    Name = Name,
                };
                list.Add(newtype);
            }
            return list;
        }
        //2.新增分类
        public static int AddNewsType(NewsType art)
        {
            string sql = string.Format(@"insert into dbo.newsType
            (Name) values('{0}')", art.Name);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        //通过Id获取数据详情
        public static NewsType GetById(string ArtId)
        {
            string sql = "select * from dbo.newsType where Id=" + ArtId;
            NewsType article = null;
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                article = new NewsType()
                {
                    Id = Id,
                    Name = Name,
                };
            }
            return article;
        }
        //编辑
        public static int EditProductType(NewsType art)
        {
            String sql = string.Format(@"UPDATE dbo.newsType
                                      SET Name ='{0}'                                     
                                       WHERE Id={1}
                                 ", art.Name, art.Id);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        //删除
        public static int DelNewsType(String Id)
        {
            //1.新闻列表
            string newslist = string.Format("delete from dbo.news where TypeId =" + Id);
            DBHelp.ExecuteNonQuery(newslist);
            //2.新闻分类表里删除
            string sql = string.Format("delete from dbo.newsType where Id =" + Id);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        //获取所有新闻的标题
        public static List<NewsType> getAllNew()
        {
            string sql = "select Id,Name from dbo.newsType";
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            List<NewsType> list = new List<NewsType>();
            while (sdr.Read())
            {
                int id = (int)sdr["Id"];
                string Name = (string)sdr["Name"];
                NewsType arc = new NewsType() { Id = id, Name = Name };
                list.Add(arc);
            }
            return list;
        }
    }
}